import React, { Component } from 'react';
import PropType from 'prop-types';
import './index.css';
export default class Footer extends Component {
  //对接收props进行：类型、必要性的限制
  static propTypes = {
    todos: PropType.array.isRequired,
    checkAllTodo: PropType.func.isRequired,
    clearAllDone: PropType.func.isRequired,
  };
  // 全选按钮
  checkAllTodo = (e) => {
    this.props.checkAllTodo(e.target.checked);
  };

  //   清除所有已完成
  clearAllTodo = () => {
    this.props.clearAllDone();
  };
  render() {
    const { todos } = this.props;
    // 已完成
    const doneCount = todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0);
    // 全部
    const total = todos.length;

    return (
      <div className='todo-footer'>
        <label>
          <input
            type='checkbox'
            checked={doneCount === total && total > 0 ? true : false}
            onChange={this.checkAllTodo}
          />
          <span>已完成{doneCount}</span>
          <span>/</span>
          <span>全部{total}</span>
        </label>
        <button onClick={this.clearAllTodo} className='btn-clear'>
          清除已完成任务
        </button>
      </div>
    );
  }
}
